/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Screens;

import Controllers.HdTipoProblemaJpaController;
import Entities.HdTipoProblema;
import Util.Constantes;
import Util.EMFConection;
import javax.swing.JOptionPane;

/**
 *
 * @author Douglas
 */
public class CadastroTipoProblema extends javax.swing.JFrame {

    /**
     * Creates new form CadastroTipoProblema
     */
    public CadastroTipoProblema() {
        initComponents();
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbDeProblema = new javax.swing.JLabel();
        spTexto = new javax.swing.JScrollPane();
        taProblema = new javax.swing.JTextArea();
        btCadastrar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        lbTitulo = new javax.swing.JLabel();
        tfTitulo = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Help Desk");
        setResizable(false);

        lbDeProblema.setText("Descrição de Problemas:");

        taProblema.setColumns(20);
        taProblema.setRows(5);
        spTexto.setViewportView(taProblema);

        btCadastrar.setText("Cadastrar");
        btCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCadastrarActionPerformed(evt);
            }
        });

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        lbTitulo.setText("Titulo do Problema:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btCadastrar)
                        .addGap(18, 18, 18)
                        .addComponent(btCancelar))
                    .addComponent(lbDeProblema)
                    .addComponent(spTexto, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
                    .addComponent(lbTitulo)
                    .addComponent(tfTitulo))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tfTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbDeProblema)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(spTexto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCadastrar)
                    .addComponent(btCancelar))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-347)/2, (screenSize.height-274)/2, 347, 274);
    }// </editor-fold>//GEN-END:initComponents

    private void btCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCadastrarActionPerformed
        HdTipoProblemaJpaController hdTipoProblemaJpaController = new HdTipoProblemaJpaController(EMFConection.getInstance());
        
        HdTipoProblema hdTipoProblema = new HdTipoProblema();
        
        if (taProblema.getText().isEmpty()){
            JOptionPane.showMessageDialog(null, Constantes.DE_PROBLEMA_EM_BRANCO);
        }else if(tfTitulo.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, Constantes.TITULO_PROBLEMA_EM_BRANCO);
        }else {
            hdTipoProblema.setDeTipoProblema(taProblema.getText());
            hdTipoProblema.setDeTituloProblema(tfTitulo.getText());
            try {
                hdTipoProblemaJpaController.create(hdTipoProblema);
                JOptionPane.showMessageDialog(null, Constantes.DADOS_INSERIDOS, Constantes.SUCESSO, JOptionPane.INFORMATION_MESSAGE);
            }catch(Exception e) {
                JOptionPane.showMessageDialog(null, Constantes.ERRO_BANCO, Constantes.ERRO, JOptionPane.ERROR_MESSAGE);
            }
            this.dispose();
        }
    }//GEN-LAST:event_btCadastrarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCadastrar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JLabel lbDeProblema;
    private javax.swing.JLabel lbTitulo;
    private javax.swing.JScrollPane spTexto;
    private javax.swing.JTextArea taProblema;
    private javax.swing.JTextField tfTitulo;
    // End of variables declaration//GEN-END:variables
}
